import axios from 'axios'
import {
	MessageBox,
	Message
} from 'element-ui'
import store from '@/store'
import {
	getToken
} from '@/utils/auth'
import router from '@/router/index'
export const baseURL= 'https://www.fastmock.site/mock/bbf5f351b99902f8d83804f71bc40a83/nightout'

const http = axios.create({
	baseURL,
	timeout: 5000
})

http.interceptors.request.use(
	config => {

		if (store.getters.token) {
			config.headers['JHC-Authorization'] = getToken()
			// config.headers['JHC-Authorization'] = 'JHCeyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiJ0ZXN0IiwiY3JlYXRlZCI6MTU3ODAyNTUyODAzNSwiZXhwIjoxNTc4MTExOTI4fQ.vBa1u-Sn5voIOsHtDYVg91RVqshY2cgM_tKEkSJTc3f1IsbtLlA20YCetJpocBcs1VyLfpKPI_dcQbrBrdA2KQ'
		}
		return config
	},
	error => {
		console.log(error)
		return Promise.reject(error)
	}
)

http.interceptors.response.use(
	response => {
		// console.log('接口返回的', response)
		const res = response.data

		if (res.code === 401) {
			MessageBox.confirm('验证失败，请重新登陆', {
				confirmButtonText: '重新登陆',
				cancelButtonText: '关闭',
				type: 'warning'
			}).then(() => {
				store.dispatch('user/clear').then(() => {
					location.reload()
				})
			})
		} else if (res.code !== 200) {

			Message({
				message: res.message || 'Error',
				type: 'error',
				duration: 5 * 1000
			})
			return Promise.reject(new Error(res.message || 'Error'))
		} else {
			return res
		}

	},
	error => {
		console.log(router)
		console.log('err' + error)
		console.log('返回来的接口问题' + error)
		Message({
			message: error.message,
			type: 'error',
			duration: 5 * 1000
		})
		return Promise.reject(error)
	}
)

export default http
